﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace CutImg {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            string fullDir = textBox1.Text.Trim();
            
            
            string dir = Path.GetDirectoryName(fullDir);
            string fileName = Path.GetFileNameWithoutExtension(fullDir);
            string ex = Path.GetExtension(fullDir);
            using (Bitmap bm = new Bitmap(fullDir)) {
                int row = bm.Height;
                int column = bm.Width;
                if (!(HScan(bm, row / 2)&&VScan(bm,column/2))) {
                    MessageBox.Show("error");
                    return;
                }
                int up = GetUp(bm);
                int down = GetDown(bm);
                int left = GetLeft(bm);
                int right = GetRight(bm);
                using (Image newBm = new Bitmap(right - left, down - up)) {
                    Graphics gp = Graphics.FromImage(newBm);
                    gp.DrawImage(bm,new Rectangle(0,0,newBm.Width,newBm.Height),left,up,newBm.Width,newBm.Height,GraphicsUnit.Pixel);
                    newBm.Save(dir+"\\"+ fileName + Guid.NewGuid().ToString() + ex);
                    
                }
                MessageBox.Show("ok");


            }
        }

        private bool HScan(Bitmap bm, int y) {
            int column = bm.Width;
            for (int i = 0; i < column-1; i++) {
                var colorLeft = bm.GetPixel(i, y).ToArgb();
                var colorRight = bm.GetPixel(i+1, y).ToArgb();
                if (Math.Abs(colorLeft-colorRight) >= 100) {
                    return true;
                }
            }
            return false;
        
        
        }
        private bool VScan(Bitmap bm, int x) {
            int row = bm.Height;
            for (int i = 0; i < row - 1; i++) {
                //var c1 = bm.GetPixel(x, i);
                //var c2 = bm.GetPixel(x, i+1);
                var colorUp = bm.GetPixel(x, i).ToArgb();
                var colorDown = bm.GetPixel(x, i+1).ToArgb();
                if (Math.Abs(colorUp - colorDown) >= 100) {
                    return true;
                }
            }
            return false;


        }
        private int GetUp(Bitmap bitmap) {
            int u = 0;
            int d = bitmap.Height / 2;
            while (d-u>20) {
                int current = (u + d) / 2;
                if (HScan(bitmap,current) ){
                    d = current;
                }
                else {
                    u = current;
                }
               
            }
            return u;

        
        }
        private int GetDown(Bitmap bitmap) {
            int u = bitmap.Height / 2;
            int d = bitmap.Height;
            while (d - u > 20) {
                int current = (u + d) / 2;
                if (HScan(bitmap, current) ){
                    u = current;
                }
                else {
                    d = current;
                }

            }
            return d;


        }
        private int GetLeft(Bitmap bitmap) {
            int l = 0;
            int r = bitmap.Width/2;
            while (r - l > 20) {
                int current = (r + l) / 2;
                if (VScan(bitmap, current)) {
                    r = current;
                }
                else {
                    l = current;
                }

            }
            return l;


        }
        private int GetRight(Bitmap bitmap) {
            int l = bitmap.Width / 2;
            int r = bitmap.Width;
            while (r - l > 20) {
                int current = (r + l) / 2;
                if (VScan(bitmap, current)) {
                    l = current;
                }
                else {
                    r = current;
                }

            }
            return r;


        }

    }
}
